﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using T8task2;
using System.Data.SqlClient;
using System.Data;

namespace T8Task2
{
    public partial class Form1 : Form
    {
      

        public static DataTable dt = new DataTable();
        public Form1()
        {
            InitializeComponent();
        }
       
       

        private void Form1_Load(object sender, EventArgs e)
        {

            //显示treeview节点
            treeView1.ExpandAll();
            //显示dataGridView
            string sql= "select * from[Products]";
            dt = DBHelper.dataTable(sql);
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = dt;

            //数据填充

           DBHelper.Cnndatabase();
            Showcategorylist();
            ShowGameList();

        }
        DataSet ds = new DataSet();
        //显示游戏类别列表篇
        private static void Showcategorylist()
        {
            DataSet ds = new DataSet();
            //获取游戏类别数据
            DataTable dt = ds.Tables[0];
            //创建根节点
            TreeNode root = new TreeNode("游戏类别");
            root.Tag = 0;//保存编号
            //将根节点连接到TreeView上
            foreach(DataRow row in dt.Rows)
            {
                string name = Convert.ToString(row["Name"]);
                //创建新类别节点
                TreeNode node = new TreeNode(name);
                //保存游戏编号
                node.Tag = Convert.ToInt32(row["ID"]);
                root.Nodes.Add(node);

            }
        }
        private void ShowGameList()
        {
            //获取游戏数据库
        }
        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("您确定要删除此行数据吗？", "删除提示", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                int number = dataGridView1.CurrentRow.Index;
                dataGridView1.Rows.RemoveAt(number);
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //获取树节点
            TreeNode node = e.Node;
            //获取类型的编号
            int id = Convert.ToInt32(node.Tag);
            if (id != 0){
                DataView dv = ds.Tables[1].DefaultView;
                //根据类别ID筛选游戏数据
                dv.RowFilter = string.Format("ID={0}",id);
                this.dataGridView1.DataSource = dv;
            }
        }
    }
}
